<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>租赁管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="carFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="carFormData.carName" placeholder="请输入汽车品牌"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="carName"
                            label="车名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="tbCarType.typeName"
                            label="种类">
                    </el-table-column>
                    <el-table-column
                            prop="tbBrand.name"
                            label="品牌">
                    </el-table-column>
                    <el-table-column
                            prop="tbCarTypeConfigDetails.typename"
                            label="配置">
                    </el-table-column>
                    <el-table-column
                            prop="intro"
                            label="排量">
                    </el-table-column>
                    <el-table-column
                            prop="vin"
                            label="车牌号">
                    </el-table-column>
                    <el-table-column
                            prop="buyPrice"
                            label="进价">
                    </el-table-column>
                    <el-table-column
                            prop="tbShop.sname"
                            label="4s店">
                    </el-table-column>
                    <el-table-column
                            prop="state"
                            :formatter="zhuangtaima"
                            label="状态">
                    </el-table-column>
                    <el-table-column
                            prop="price"
                            label="租价">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.id)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
<!--        提示框-->
        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                :before-close="handleClose"
        >

            <!--            model:表单数据-->
            <el-form  label-width="80px" :model="carForm"  :rules="insertFormRules"  ref="insertFormRef">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="carForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="车名" prop="carName">
                    <el-input v-model="carForm.carName"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select v-model="carForm.type" placeholder="请选择配置">
                        <el-option
                                v-for="item in typeArray"
                                :key="item.id"
                                :label="item.typeName"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="品牌">
                    <el-select v-model="carForm.brandId" placeholder="请选择配置">
                        <el-option
                                v-for="items in brandsArray"
                                :key="items.id"
                                :label="items.name"
                                :value="items.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="配置">
                    <el-select v-model="carForm.configId" placeholder="请选择配置">
                        <el-option
                                v-for="itemss in configArray"
                                :key="itemss.id"
                                :label="itemss.typename"
                                :value="itemss.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店">
                    <el-select v-model="carForm.shopId" placeholder="请选择配置">
                        <el-option
                                v-for="itemsss in shopArray"
                                :key="itemsss.id"
                                :label="itemsss.sname"
                                :value="itemsss.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排量" prop="intro" >
                    <el-input v-model="carForm.intro"></el-input>
                </el-form-item>
                <el-form-item label="车辆识别码" prop="vin">
                    <el-input v-model="carForm.vin"></el-input>
                </el-form-item>
                <el-form-item label="进价" prop="buyPrice" >
                    <el-input v-model="carForm.buyPrice"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="carForm.state" placeholder="请选择配置">
                        <el-option
                                v-for="itemes in statusArray"
                                :key="itemes.id"
                                :label="itemes.status"
                                :value="itemes.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="租价" prop="price" >
                    <el-input v-model="carForm.price"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>

              </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Allcar",
        data(){


            /* 车牌名唯一校验*/
            var nameValidator = (rule, value, callback) => {
                this.$http.post("lease/car/selectVin",this.carForm).then(res=> {
                    if (res.data.code==5000){
                        callback(res.data.message)
                    }else {
                        callback();
                    }
                });
            };
            return {
                insertFormRules:{
                    carName:[
                        { required: true, message: '请输入汽车名称', trigger: 'blur' }
                    ],
                    intro:[
                        { required: true, message: '请输入简介', trigger: 'blur' },
                    ] ,
                    vin:[
                        { required: true, message: '请输入车辆识别码', trigger: 'blur' },
                        {pattern: /^[a-zA-Z][A-Za-z0-9]+$/, message: '请输入正确的车辆识别码', trigger: 'change'},
                        { validator: nameValidator, trigger: 'change' },

                    ],
                    buyPrice:[
                        { required: true, message: '请输入购买价格', trigger: 'blur' },
                        // { type: 'number', message: '购买价格必须为数字值', trigger: 'change'},
                        {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入正确的价格', trigger: 'change'},

                    ],
                    price:[
                        { required: true, message: '请输入价格', trigger: 'blur' },
                        // { type: 'number', message: '价格必须为数字值',trigger: 'change'},
                        {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入正确的价格', trigger: 'change'},
                        // {pattern:  /^[0-9]*[1-9][0-9]*$/, message: '请输入正确的价格', trigger: 'change'}
                    ]

                },

                editDialogVisible:false,
                //条件搜索对象
                carFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //添加表:
                carForm:{},
                configArray:[],
                brandsArray:[],
                shopArray:[],
                typeArray:[],
                statusArray:[{id:1,status:"可租"},{id:2,status:"维修中"},{id:3,status:"租出"},{id:4,status:"不可用"}],

            }
        },
        created() {
            this.selectAllCar();
            this.allselectlist(); //全部下拉框
        },
        methods:{
            //重置表单属性
            // restCarFrom(){
            //     this.$refs.insertFormRef.resetFields();
            // },

            insert(){
                this.editDialogVisible=true;
                // this.$refs.insertFormRef.resetFields();
                this.carForm={};

            },
            deleteById(id){
                var that=this;
                this.$http.get(`lease/car/deleteCar/${id}`).then(function (returnResult) {
                    that.$message.success("删除成功");
                    that.selectAllCar();
                    // location.reload();
                })
                this.currentPage=1;
            },
            selectById(id){
                var that=this;
                console.log(id+"--------------------------")
                that.editDialogVisible=true;
                // this.$refs.insertFormRef.resetFields();

                //var idd = row.id;
                this.$http.get(`/lease/car/selectCarById/${id}`).then(function (r) {
                    that.carForm=r.data.result.tbCar;


                })

            },
            formSubmit() {
                this.$refs.insertFormRef.validate((valid) => {
                    if (valid) {
                        var that = this;
                        if (this.carForm.id != null) {
                            this.$http.put("lease/car/updateCarById", this.carForm).then(function (returnResult) {
                                that.editDialogVisible = false;
                                that.carForm = {};
                                that.selectAllCar();
                            })
                        } else {
                            this.$http.put("lease/car/insertCar", this.carForm).then(function (returnResult) {
                                that.editDialogVisible = false;
                                that.selectAllCar();
                            })
                        }
                    }

                })
            },

                allselectlist(){
                    var that = this;
                    this.$http.get("lease/carConfig/selectAllCarConfig").then(function (returnResult) {
                        that.configArray = returnResult.data.result.AlltbConfigs;
                    })
                    this.$http.get("/lease/carBrand/selectAllCarBrand").then(function (returnResult) {
                        that.brandsArray = returnResult.data.result.AlltbBrands;

                    })
                    this.$http.get("/lease/carType/selectAllCarType").then(function (returnResult) {
                        that.typeArray = returnResult.data.result.AlltbTypes;
                    })
                    this.$http.get("/lease/carShop/selectAllCarShop").then(function (returnResult) {
                        that.shopArray = returnResult.data.result.AlltbShops;
                    })
            },
            chaxun(){
                this.selectAllCar();
            },
            selectAllCar(){
                var that=this;
                this.$http.post(`/lease/car/initCarTable/${this.currentPage}/${this.pageSize}`,this.carFormData).then(function (returnResult) {

                    that.tableData=returnResult.data.result.records;
                    that.total=returnResult.data.result.total;
                })
            },
            // 改变条数
            handleSizeChange(v){
                this.pageSize=v;
                this.selectAllCar();
            },
            //改变页码
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectAllCar();
            },
            //改变状态码
            zhuangtaima(row,column){
                var status=row.state;
                if(status==1){
                    return '可租'
                }
                if(status==2){
                    return '维修中'
                }
                if(status==3){
                    return '租出'
                }
                if(status==4){
                    return '损坏'
                }
            },
            handleClose()
            {
                this.editDialogVisible=false;
                this.$refs.insertFormRef.resetFields();
            }

        }
    }
</script>

<style scoped>

</style>